﻿using System;

namespace rpxlogindemo.extensionMethods
{
    public static class DateTimes
    {
        /// <summary>
        /// Turns DateTime to "happened this long ago" style wording.
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static string ToPretty(this DateTime d)
        {
            //http://dotnetperls.com/Content/Pretty-Date.aspx
            string retval = string.Empty;

            if (d > DateTime.MinValue)
            {
                TimeSpan s = DateTime.Now.Subtract(d);

                if (s.Days == 0)
                {
                    if (s.Minutes < 1)
                        retval = "a moment ago";
                    else if (s.Minutes < 2)
                        retval = "a minute ago";
                    else if (s.Hours < 1)
                        retval = String.Format("{0} minutes ago", s.Minutes);
                    else if (s.Hours < 2)
                        retval = "an hour ago";
                    else
                        retval = String.Format("{0} hours ago", s.Hours);
                }
                else if (s.Days == 1)
                    retval = "yesterday";
                else if (s.Days < 7)
                    retval = String.Format("{0} days ago", s.Days);
                else if (s.Days < 14)
                    retval = "a week ago";
                else
                    retval = String.Format("{0} weeks ago", Math.Floor((double)s.Days / 7));
            }
            return retval;
        }
    }
}